package com.lst.servlet;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.ClientReqPara;
import com.lst.entity.response.ClientResPara;
import com.lst.model.MstClient;
import com.lst.service.MstClientService;
import com.lst.utils.RequestUtils;

/**
 * @ClassName: BindThirdServlet
 * @Description: 绑定第三方登陆
 * @author zmm
 * @date 2017.10.18
 * 
 */
public class BindThirdServlet extends BaseServlet {

    private static final long serialVersionUID = 1L;

    private MstClientService mstClientService = ctx
	    .getBean(MstClientService.class);

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
	doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
	Date startDate = new Date();

	ClientResPara resPara = new ClientResPara();

	try {
	    ClientReqPara reqPara = getReqPara(request);

	    if (isCheckField(reqPara, resPara)) {
		Integer userid = Integer.valueOf(reqPara.getUserid());
		String nickname = reqPara.getNickname();
		String portrait = reqPara.getPortrait();
		String thirdno = reqPara.getThirdno();
		String logintype=reqPara.getLogintype();
		
		Map<String, Object> reqMap = new HashMap<String, Object>();

		if(CommCode.SYSTEM_LOGINTYPE_WEIXIN.toString().equals(logintype)){
			reqMap.put("thirdNo", thirdno);
		}
		if(CommCode.SYSTEM_LOGINTYPE_QQ.toString().equals(logintype)){
			reqMap.put("thirdNoqq", thirdno);
		}		
		if(CommCode.SYSTEM_LOGINTYPE_SINA.toString().equals(logintype)){
			reqMap.put("thirdNowb", thirdno);
		}
		reqMap.put("enabled", true);
		reqMap.put("isDeleted", false);
		reqMap.put("id", userid);
		
		List<MstClient> list = mstClientService.queryByList(reqMap,new PageBounds());
		
		if(list!=null&&list.size()>0){
			resPara.setCode(CommCode.M_Y000000);
			resPara.setMessage(CommCode.M_C000006);
		}else{
			MstClient mstclient = new MstClient();
			mstclient.setId(userid);
			mstclient.setNickname(nickname);
			mstclient.setPortrait(portrait);
			mstclient.setReserved11(portrait);
			if(CommCode.SYSTEM_LOGINTYPE_WEIXIN.toString().equals(logintype)){
				mstclient.setThirdno(thirdno);
			}
			if(CommCode.SYSTEM_LOGINTYPE_QQ.toString().equals(logintype)){
				mstclient.setThirdnoqq(thirdno);
			}		
			if(CommCode.SYSTEM_LOGINTYPE_SINA.toString().equals(logintype)){
				mstclient.setThirdnowb(thirdno);
			}
			mstclient.setUpdatedate(startDate);
			mstclient.setUpdateuser(userid);

			mstClientService.updateByPrimaryKeySelective(mstclient);

			MstClient mc = mstClientService.selectByPrimaryKey(userid);

			resPara.setCode(CommCode.M_Y000000);
			resPara.setMessage(CommCode.M_Y000001);
			resPara.setMstClient(mc);
		}

	    }

	} catch (Exception e) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_SYS0001);

	    logger.error("doPost error: ", e);
	}

	try {
	    response.setContentType("application/json;charset=utf-8");
	    response.setHeader("pragma", "no-cache");
	    response.setHeader("cache-control", "no-cache");

	    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
		    .excludeFieldsWithoutExposeAnnotation().serializeNulls()
		    .create();
	    String jsonResult = gson.toJson(resPara);

	    PrintWriter out = response.getWriter();
	    out.print(jsonResult);
	    out.flush();
	    out.close();

	} catch (Exception e) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_SYS0003);

	    logger.error("doPost error: ", e);
	}
    }

    @Override
    public ClientReqPara getReqPara(HttpServletRequest request) {

	ClientReqPara reqPara = null;

	try {
	    BaseRequest baseRequest = RequestUtils.getRequestPara(request,
		    new ClientReqPara());

	    reqPara = (ClientReqPara) baseRequest.clone();

	    String nickname = request.getParameter("nickname");
	    String portrait = request.getParameter("portrait");
	    String thirdno = request.getParameter("thirdno");
	    String userid = request.getParameter("userid");
	    String logintype = request.getParameter("logintype");

//	    logger.info("reqPara nickname: " + nickname);
//	    logger.info("reqPara portrait: " + portrait);
//	    logger.info("reqPara thirdno: " + thirdno);
//	    logger.info("reqPara userid: " + userid);
//	    logger.info("reqPara logintype: " + logintype);

	    reqPara.setNickname(nickname);
	    reqPara.setPortrait(portrait);
	    reqPara.setThirdno(thirdno);
	    reqPara.setUserid(userid);
	    reqPara.setLogintype(logintype);

	} catch (Exception e) {
	    logger.error("getReqPara error: ", e);
	}

	return reqPara;
    }

    @Override
    public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
	boolean flag = false;

	ClientReqPara bmReqPara = (ClientReqPara) reqPara;
	ClientResPara bmResPara = (ClientResPara) resPara;

	if (StringUtils.isEmpty(bmReqPara.getUserid())) {
	    bmResPara.setCode(CommCode.M_A000000);
	    bmResPara.setMessage(CommCode.M_P000002);
	}else if (StringUtils.isEmpty(bmReqPara.getThirdno())) {
	    bmResPara.setCode(CommCode.M_A000000);
	    bmResPara.setMessage(CommCode.M_C000025);
	}  else {
	    flag = true;
	}

	return flag;
    }
}
